package com.zpark.mybatisplus.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zpark.mybatisplus.bean.Ranking;
import com.zpark.mybatisplus.mapper.RankingMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("mybatisplus")
public class RankingController {

    @Autowired
    public RankingMapper rankingMapper;

    @RequestMapping("findAll")
    public PageInfo<Ranking> findALl(@RequestParam(defaultValue = "0", value = "pageNum") Integer pageNum, @RequestParam(defaultValue = "5", value = "pageSize") Integer pageSize){
        PageHelper.startPage(pageNum, pageSize);
        List<Ranking> result = rankingMapper.selectList(null);
        PageInfo<Ranking> pageInfo = new PageInfo<>(result);
        return pageInfo;
    }

    @RequestMapping("findById/{id}")
    public Ranking findById(@PathVariable("id") Integer id){
        return rankingMapper.selectById(id);
    }

    @RequestMapping("findBykey/{key}")
    public List<Ranking> findByKey(@PathVariable("key") String key){
        return rankingMapper.selectList(new QueryWrapper<Ranking>().like(false, "key", key));
    }

    @RequestMapping("updateById/{id}")
    public int update(@PathVariable("id") Integer id, @RequestParam("film_index") Integer filmIndex, @RequestParam("film_name") String filmName, @RequestParam("film_actor_name") String filmActorName, @RequestParam("film_feasibly") String filmFeasibly){
        Ranking ranking = new Ranking();
        return rankingMapper.update(ranking, new UpdateWrapper<Ranking>().eq("id", id)
                .set("film_index", filmIndex)
                .set("film_name", filmActorName)
                .set("film_actor_name", filmActorName)
                .set("film_feasibly", filmFeasibly));
    }

    @RequestMapping("delectById/{id}")
    public int delectById(@PathVariable("id") Integer id){
        return rankingMapper.deleteById(id);
    }

    @RequestMapping("zdy/select")
    public List<Ranking> key(@RequestParam("key") String key){
        return rankingMapper.selectKeyByFilmName(key);
    }
}
